const WorkerThread = require("./worker");
const _workerThread = new WorkerThread();
const { url, elements } = require("./config/ncep.config");
const { intervalTimer } = require("./config/config");
const schedule = require("node-schedule");
const moment = require("moment");
const colors = require("colors");

const noLevels = [{ key: "all", name: "全部" }];
const Create = () => {
  let _levels = [];
  for (const { key, name, level } of elements) {
    _levels = level
      ? level.map((i) => {
          return { key: i.key, name: i.name };
        })
      : noLevels;
    // const startTime = moment().format("YYYYMMDD");
    const startTime = moment().subtract(1,"day").format("YYYYMMDD");

    _workerThread.add({
      startTime,
      eleKey: key,
      levels: _levels,
      times: url.times,
    });
  }
};

const RunWorker = () => {
  Create();
  _workerThread.stop().run();
};

const Task = () => {
  console.log(
    colors.green(
      `${moment().format("YYYY-MM-DD HH:mm:ss")}定时任务启执行中....`
    )
  );
  // RunWorker();
    schedule.scheduleJob(intervalTimer, () => {
      console.log(
        colors.green("定时任务执行" + moment().format("YYYY-MM-DD HH:mm:ss"))
      );
      RunWorker();
    });
};

Task();
